﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    public partial class Payment
    {
        private VoucherRepository voucherRepository = new VoucherRepository();

        private bool isValid = true;
        private string errorMessage = string.Empty;

        public bool IsValid
        {
            get { return isValid; }
        }

        public string ErrorMessage
        {
            get { return errorMessage; }
        }

        public bool MakePayment(Payment payment, Guest guest)
        {
            // Check Amount
            if (payment.Amount != null && payment.Amount.Value < 1)
            {
                SetError("Invalid payment amount.");
            }
            else
            {
                Amount = payment.Amount;
            }

            
            if (ValidateCreditCard(guest.CreditNumber, guest.ExpirationDate.Value, guest.FirstName + " " + guest.LastName))
            {
                if (this.isValid)
                {
                    this.TransactionNumber = PaymentServiceCall(guest.CreditNumber, guest.ExpirationDate.Value, guest.FirstName + " " + guest.LastName, payment.Amount.Value);
                }
            }
            else
            {
                SetError("Invalid credit card information.");
            }

            if (payment.Voucher != null && string.IsNullOrEmpty(payment.Voucher.VoucherNumber))
            {
                Voucher voucher = voucherRepository.FindByVourcherNumber(payment.Voucher.VoucherNumber);
                if (voucher != null && voucher.VoucherStatus == (int) VoucherStatus.Valid)
                {
                    if (voucher.Amount < payment.Amount)
                    {
                        SetError("Voucher amount not enough.");
                    }
                    else
                    {
                        if (this.isValid)
                        {
                            voucher.VoucherStatus = (int)VoucherStatus.Used;
                            voucherRepository.Commit();
                        }
                    }
                }
            }

            if (isValid)
            {
                PaymentDate = DateTime.Now;
            }
            
            return this.isValid;
        }

        private void SetError(string message)
        {
            isValid = false;
            errorMessage += message + Environment.NewLine;
        }

        private string PaymentServiceCall(string creditNumber, DateTime expiryDate, string creditCardHolderName, decimal amount)
        {
            // Make a payment service call
            return new Random().Next(10000000, 99999999).ToString();
        }

        private bool ValidateCreditCard(string creditNumber, DateTime expiryDate, string creditCardHolderName)
        {
            // This method will call credit card bank and check
            // 1. Credit Card Number
            // 2. Expiry Date
            // 3. Credit Card Holder Name
            // 4. Available Credit
            return true;
        }
    }
}
